package com.power.sifa.approve.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.plf.common.domain.PageDataResponse;
import com.plf.core.common.service.BaseServiceImpl;
import com.power.sifa.approve.dao.Apl_notepadDao;
import com.power.sifa.approve.model.Apl_notepad;
import com.power.sifa.approve.model.pageInfo.Apl_notepadPageInfo;
import com.power.sifa.approve.service.Apl_notepadService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 *  行政复议记事本信息业务层实现
 */
@Service
public class Apl_notepadServiceImpl extends BaseServiceImpl implements Apl_notepadService {

    @Autowired
    private Apl_notepadDao dao;

    @Override
    public Apl_notepad getById(Serializable id) {
        return dao.findById(id).orElse(null);
    }

    @Override
    public List<Apl_notepad> getByApplyId(String applyid) {
        return dao.findByApplyid(applyid);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdate(Apl_notepad apl_notepad) {
    	// save 如果数据库不存在这条数据，就会执行保存（即将这条数据插入到数据库）；如果数据库存在这条数据，就会根据主键去更新这条数据
        if (apl_notepad.getPrimaryKey() != null) {
        	 Apl_notepad apl_notepadfDb = dao.findById(apl_notepad.getPrimaryKey()).orElse(null);
             if(apl_notepadfDb != null) {
                BeanUtils.copyProperties(apl_notepad, apl_notepadfDb);
                dao.save(apl_notepadfDb);
                return;
             }
        }
        dao.save(apl_notepad);
    }

    @Override
    public void removeById(Serializable id) {
        dao.deleteById(id);
    }

    @Override
    public PageDataResponse findAll(Apl_notepadPageInfo pageInfo) {
        List<Apl_notepad> list = this.dao.findAll(pageInfo);
        //分页
        PageDataResponse page = new PageDataResponse();
        if(CollectionUtil.isNotEmpty(list)){
            for(Apl_notepad data : list){
                data.geneText();
            }
            Long total = Long.valueOf(pageInfo.getAllcount());
            int size = pageInfo.getPageSize() * pageInfo.getPageNumber();
            if(total.intValue() - size > 0){
                page.setNextPage(pageInfo.getPageNumber() + 1);
            }
            page.setSize(pageInfo.getPageSize());
            page.setCurrent((pageInfo.getPageNumber()));
            page.setTotal(total);
        } else {
            page.setSize(0);
            page.setCurrent(1);
            page.setTotal(0L);
        }
        page.setRecords(list);
        return page;
    }
    
    @Transactional(rollbackFor = Exception.class)
    public void removeByIds(List<Integer> ids) {
        dao.removeByIds(ids);
    }
    
    @Override
	public List<Apl_notepad> findAll() {
		return dao.findAll();
	}
	
	@Override
	public void saveAll(List<Apl_notepad> list) {
		dao.saveAll(list);
	}
}
